package cn.itcast.order.service;


import cn.itcast.demo.client.UserClient;
import cn.itcast.demo.pojo.User;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;


    @Autowired
    private UserClient userClient;

    @SentinelResource("queryOrderById")
    public Order queryOrderById(Long orderId) {
        log.info("queryOrderById,基于ID查询商品");
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 2. 通过restTemplate 用户服务
//        String url = "http://userservice/user/"+order.getUserId();
//        User user = restTemplate.getForObject(url, User.class);
        User user = userClient.findById(order.getUserId());
        // 3.封装到order对象
        order.setUser(user);
        updateOrder();
        // 4.返回
        return order;
    }

    @SentinelResource("updateOrder")
    public void updateOrder(){
        log.info("updateOrder....");
    }
}
